import math
import time

WIDTH = 300
HEIGHT = 300

BACK_COLOR = (0, 0, 127)
CIRCLE_COLOR = (0, 200, 0)

x, y = 50, 100
r = 20

speed = 100 
dirx, diry = 0, 0

lastTime = time.time()

def get_dir(x1, y1, x2, y2):
    r = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    return (x2 - x1) / r, (y2 - y1) / r

def on_key_down(key):
    if key == keys.ESCAPE:
        quit() 
        
def on_mouse_down(pos):
    print("mouse_down", pos)
    global dirx, diry
    dirx, diry = get_dir(x, y, pos[0], pos[1])

    
def on_mouse_up(pos):
    print("mouse_up", pos)
    
def draw_circle(x, y, r):
    screen.draw.filled_circle((x, y), r, CIRCLE_COLOR)
    return 1
    
def update():
    global x, y
    global dirx, diry
    global lastTime
    
    dt = time.time() - lastTime
    lastTime = time.time()
    dx = speed * dirx * dt
    dy = speed * diry * dt

    if x + dx + r < WIDTH and x + dx - r > 0 and y + dy + r < HEIGHT and y + dy - r > 0:
        x += dx
        y += dy
        
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    draw_circle(x, y, r)